package org.loofer.ext.utils.security;

public class TEA {
    static final /* synthetic */ boolean $assertionsDisabled = (!TEA.class.desiredAssertionStatus());
    private static final int CUPS = 32;
    private static final int SUGAR = -1640531527;
    private static final int UNSUGAR = -957401312;
    private int[] S;

    public TEA(byte[] key) {
        this.S = new int[4];
        if (key == null) {
            throw new RuntimeException("Invalid key: Key was null");
        } else if (key.length < 16) {
            throw new RuntimeException("Invalid key: Length was less than 16 bytes");
        } else {
            int off = 0;
            for (int i = 0; i < 4; i++) {
                int off2 = off + 1;
                off = off2 + 1;
                off2 = off + 1;
                off = off2 + 1;
                this.S[i] = (((key[off] & 255) | ((key[off2] & 255) << 8)) | ((key[off] & 255) << 16)) | ((key[off2] & 255) << 24);
            }
        }
    }

    public TEA(int[] key) {
        this.S = new int[4];
        this.S = key;
    }

    public byte[] encrypt(byte[] clear) {
        int i;
        int length = clear.length / 8;
        if (clear.length % 8 == 0) {
            i = 0;
        } else {
            i = 1;
        }
        int[] buffer = new int[(((i + length) * 2) + 1)];
        buffer[0] = clear.length;
        pack(clear, buffer, 1);
        brew(buffer);
        return unpack(buffer, 0, buffer.length * 4);
    }

    public byte[] decrypt(byte[] crypt) {
        if (!$assertionsDisabled && crypt.length % 4 != 0) {
            throw new AssertionError();
        } else if ($assertionsDisabled || (crypt.length / 4) % 2 == 1) {
            int[] buffer = new int[(crypt.length / 4)];
            pack(crypt, buffer, 0);
            unbrew(buffer);
            return unpack(buffer, 1, buffer[0]);
        } else {
            throw new AssertionError();
        }
    }

    void brew(int[] buf) {
        if ($assertionsDisabled || buf.length % 2 == 1) {
            for (int i = 1; i < buf.length; i += 2) {
                int v0 = buf[i];
                int v1 = buf[i + 1];
                int sum = 0;
                int n = 32;
                while (true) {
                    int n2 = n - 1;
                    if (n <= 0) {
                        break;
                    }
                    sum -= 1640531527;
                    v0 += ((((v1 << 4) + this.S[0]) ^ v1) + ((v1 >>> 5) ^ sum)) + this.S[1];
                    v1 += ((((v0 << 4) + this.S[2]) ^ v0) + ((v0 >>> 5) ^ sum)) + this.S[3];
                    n = n2;
                }
                buf[i] = v0;
                buf[i + 1] = v1;
            }
            return;
        }
        throw new AssertionError();
    }

    void unbrew(int[] buf) {
        if ($assertionsDisabled || buf.length % 2 == 1) {
            for (int i = 1; i < buf.length; i += 2) {
                int v0 = buf[i];
                int v1 = buf[i + 1];
                int sum = UNSUGAR;
                int n = 32;
                while (true) {
                    int n2 = n - 1;
                    if (n <= 0) {
                        break;
                    }
                    v1 -= ((((v0 << 4) + this.S[2]) ^ v0) + ((v0 >>> 5) ^ sum)) + this.S[3];
                    v0 -= ((((v1 << 4) + this.S[0]) ^ v1) + ((v1 >>> 5) ^ sum)) + this.S[1];
                    sum += 1640531527;
                    n = n2;
                }
                buf[i] = v0;
                buf[i + 1] = v1;
            }
            return;
        }
        throw new AssertionError();
    }

    void pack(byte[] src, int[] dest, int destOffset) {
        if ($assertionsDisabled || (src.length / 4) + destOffset <= dest.length) {
            int shift = 24;
            int j = destOffset;
            dest[j] = 0;
            for (byte b : src) {
                dest[j] = dest[j] | ((b & 255) << shift);
                if (shift == 0) {
                    shift = 24;
                    j++;
                    if (j < dest.length) {
                        dest[j] = 0;
                    }
                } else {
                    shift -= 8;
                }
            }
            return;
        }
        throw new AssertionError();
    }

    byte[] unpack(int[] src, int srcOffset, int destLength) {
        if ($assertionsDisabled || destLength <= (src.length - srcOffset) * 4) {
            byte[] dest = new byte[destLength];
            int i = srcOffset;
            int count = 0;
            for (int j = 0; j < destLength; j++) {
                dest[j] = (byte) ((src[i] >> (24 - (count * 8))) & 255);
                count++;
                if (count == 4) {
                    count = 0;
                    i++;
                }
            }
            return dest;
        }
        throw new AssertionError();
    }
}
